﻿using System;
using System.Web;
using System.Text;

namespace Mammoo.Wap
{
    public class Wap
    {
        // Methods
        public void Link(string url, string name)
        {
            HttpContext.Current.Response.Write("\r\n<br/><a href=\"" + url.Replace("&", "&amp;") + "\">" + name + "</a>");
        }

        public void NewLine()
        {
            HttpContext.Current.Response.Write("<br/>\r\n");
        }

        public void Write(string str)
        {
            HttpContext.Current.Response.Write(str + "\r\n");
        }
    }

    public class Wml : Wap
    {
        // Fields
        private StringBuilder sbp = new StringBuilder();

        // Methods
        public Wml()
        {
            this.Start();
        }

        public void AnchorEnd()
        {
            HttpContext.Current.Response.Write("\r\n</go>\r\n</anchor>");
        }

        public void AnchorStart(string url, string name)
        {
            HttpContext.Current.Response.Write("\r\n<anchor>" + name);
            HttpContext.Current.Response.Write("\r\n<go href=\"" + url.Replace("&", "&amp;") + "\" method=\"post\">\r\n");
            HttpContext.Current.Response.Write(this.sbp);
        }

        public void End()
        {
            HttpContext.Current.Response.Write("\r\n</p>");
            HttpContext.Current.Response.Write("\r\n</card>");
            HttpContext.Current.Response.Write("\r\n</wml>");
            HttpContext.Current.Response.End();
        }

        public void Head(string title)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
            builder.Append("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">\r\n");
            builder.Append("<wml>\r\n");
            builder.Append("<head>\r\n");
            builder.Append("<meta http-equiv=\"Cache-Control\" content=\"max-age=0\" forua=\"true\" />\r\n");
            builder.Append("</head>\r\n");
            builder.Append("<card title=\"" + title + "\" newcontext=\"true\">\r\n");
            builder.Append("<p>\r\n");
            HttpContext.Current.Response.Write(builder);
        }

        public void InputBox(string name, string lable, bool separator, string value, string length, string empty, string format)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(lable);
            if (separator)
            {
                builder.Append(":");
            }
            else
            {
                builder.Append("<br/>");
            }
            builder.Append("<input type=\"text\"");
            builder.Append(" name=\"" + name + "\"");
            builder.Append(" value=\"" + value + "\"");
            builder.Append(" maxlength=\"" + length + "\"");
            builder.Append(" emptyok=\"" + empty + "\"");
            if (format != "")
            {
                builder.Append(" format=\"" + format + "\"");
            }
            builder.Append("/><br/>\r\n");
            this.sbp.Append("<postfield name=\"" + name + "\" value=\"$" + name + "\"/>\r\n");
            HttpContext.Current.Response.Write(builder);
        }

        public void Postfield(string name, string value)
        {
            HttpContext.Current.Response.Write("<postfield name=\"" + name + "\" value=\"$" + value + "\"/>\r\n");
        }

        public void Start()
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "text/vnd.wap.wml;charset=UTF-8";
        }
    }

    public class Xhtml : Wap
    {
        // Methods
        public Xhtml()
        {
            this.Start();
        }

        public void Button(string name)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("\r\n<input type=\"submit\" value=\"" + name + "\"><br/>");
            HttpContext.Current.Response.Write(builder);
        }

        public void End()
        {
            HttpContext.Current.Response.Write("\r\n</body>");
            HttpContext.Current.Response.Write("\r\n</html>");
            HttpContext.Current.Response.End();
        }

        public void FileBox(string name, string lable, bool separator)
        {
            StringBuilder builder = new StringBuilder();
            if (lable != "")
            {
                builder.Append(lable);
                if (separator)
                {
                    builder.Append(":");
                }
                else
                {
                    builder.Append("<br/>");
                }
            }
            builder.Append("<input type=\"file\" Name=\"" + name + "\"/><br/>");
            HttpContext.Current.Response.Write(builder);
        }

        public void FormEnd()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("\r\n</form>");
            HttpContext.Current.Response.Write(builder);
        }

        public void FormStart(string url)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<form method=\"POST\" action=\"" + url.Replace("&", "&amp;") + "\"");
            builder.Append(" name=\"xhtmlform\" >\r\n");
            HttpContext.Current.Response.Write(builder);
        }

        public void FormStart(string url, int i)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<form method=\"POST\" action=\"" + url.Replace("&", "&amp;") + "\"");
            builder.Append(" enctype=\"multipart/form-data\"");
            builder.Append(" name=\"xhtmlform\" >\r\n");
            HttpContext.Current.Response.Write(builder);
        }

        public void Head(string title)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
            builder.Append("<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.0//EN\" \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">\r\n");
            builder.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
            builder.Append("<head>\r\n");
            builder.Append("<meta http-equiv=\"Cache-Control\" content=\"max-age=0\" forua=\"true\" />\r\n");
            builder.Append("<title>" + title + "</title>\r\n");
            builder.Append("</head>\r\n");
            builder.Append("<body>\r\n");
            HttpContext.Current.Response.Write(builder);
        }

        public void InputBox(string name, string lable, bool separator, string value, string length)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(lable);
            if (separator)
            {
                builder.Append(":");
            }
            else
            {
                builder.Append("<br/>");
            }
            builder.Append("<input type=\"text\"");
            builder.Append(" name=\"" + name + "\"");
            builder.Append(" value=\"" + value + "\"");
            builder.Append(" maxlength=\"" + length + "\"");
            builder.Append("/><br/>\r\n");
            HttpContext.Current.Response.Write(builder);
        }

        public void Start()
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/vnd.wap.xhtml+xml";
        }
    }
}
